<html>
<head>
    <meta charset="utf-8">
    <title>新建/编辑 角色</title>
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <link rel="stylesheet" href="/css/font.css">
    <link rel="stylesheet" href="/css/xadmin.css">

    <!-- <link rel="stylesheet" href="/css/theme5.css"> -->
    <script src="/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                        <legend>新建/编辑 角色</legend>
                    </fieldset>
                </div>
                <div class="layui-card-body ">
                    <form class="layui-form layui-form-pane" action="" id="editForm">
                        <input type="hidden" name="id" id="id" value="${editData.id!}">


                        <div class="layui-form-item">
                            <input type="text" name="ename" id="ename" lay-verify="ename" autocomplete="off"
                                   placeholder="请输入英文标识"
                                   class="layui-input" value="${editData.ename!}" <%if(editData!=null &&editData.ename=="system"){%> readonly <%}%>  >
                        </div>


                        <div class="layui-form-item">
                            <input type="text" name="cname" id="cname" lay-verify="cname" autocomplete="off"
                                   placeholder="请输入中文描述"
                                   class="layui-input" value="${editData.cname!}" <%if(editData!=null &&editData.ename=="system"){%> readonly <%}%>>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-input-block" style="margin-left: 0;">
                                <label class="layui-form-label" style="width: 130px;">发表文章需审核</label>
                                <div class="layui-input-block">
                                    <input type="checkbox" name="verify" id="verify" lay-skin="switch"
                                           lay-filter="verify" lay-text="ON|OFF"
                                    <%if(editData!=null && editData.verify==true){%> checked <% }%> >
                                </div>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-input-block" style="margin-left: 0;">
                                <label class="layui-form-label" style="width: 130px;">发表评论需审核</label>
                                <div class="layui-input-block">
                                    <input type="checkbox" name="verifyCmt" id="verifyCmt" lay-skin="switch"
                                           lay-filter="verifyCmt" lay-text="ON|OFF"
                                    <%if(editData!=null && editData.verifyCmt==true){%> checked <% }%> >
                                </div>
                            </div>
                        </div>


                        <div class="layui-form-item layui-form-text">
                            <label class="layui-form-label">
                                拥有权限
                            </label>
                            <table class="layui-table layui-input-block">
                                <tbody>

                                <!--
                                       //var key = entry.key;//AuthKey
                                        //var value = entry.value;//List<AuthInfo>
                                        //print(value.name);

                                -->

                                <%for(entry in auths){
                                var key = entry.key;
                                var value = entry.value;
                                %>
                                <tr>
                                    <td>
                                        <input type="checkbox" name="${key.authId}" lay-skin="primary"
                                        <%if(key.disabled){%>disabled <%}%>
                                        lay-filter="authKey" title="${key.authCname}" <%if(key.checked){%>checked <%}%>
                                        >
                                    </td>
                                    <td>
                                        <div class="layui-input-block">

                                            <% for(data in value){ %>
                                            <input type="checkbox" name="${data.id}" lay-skin="primary"
                                            <%if(key.disabled){%>disabled <%}%>
                                            lay-filter="authKey" title="${data.cname}(${data.menuType})"
                                            <%if(data.checked){%>checked <%}%>>
                                            <%}%>
                                        </div>
                                    </td>
                                </tr>

                                <%}%>

                                </tbody>
                            </table>
                        </div>

                        <%if(editData==null||editData.ename!="system"){%>
                            <div class="layui-form-item">
                                <button type="button" class="layui-btn layui-btn-normal" lay-submit lay-filter="save">保存</button>
                            </div>
                        <%}%>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>


    layui.use(['form', 'layer', 'layedit'], function () {
        $ = layui.jquery;
        var form = layui.form;


        //自定义验证规则
        form.verify({
            ename: function (value) {
                if (value === undefined || value === '' || value.length === 0) {
                    return '英文标识不能为空';
                }
            },
            cname: function (value) {
                if (value === undefined || value === '' || value.length === 0) {
                    return '中文描述不能为空';
                }
            }

        });


        //监听提交
        form.on('submit(save)', function (data) {
            data.field.keys = Object.keys(data.field);

            if (data.field.verify === 'on') {
                data.field.verify = true;
            } else {
                data.field.verify = false;
            }

            if (data.field.verifyCmt === 'on') {
                data.field.verifyCmt = true;
            } else {
                data.field.verifyCmt = false;
            }

            $.ajax({
                cache: false,
                type: "POST",
                contentType: 'application/json',
                url: "/role/addup",
                data: JSON.stringify(data.field),
                async: false,
                success: function (data) {
                    if (data.code === 0) {
                        layer.msg(data.message, {
                            time: 2000
                        });

                        self.setTimeout(function () {
                            $("#editForm")[0].reset();
                            layui.form.render();
                            $("#cname").val('');
                            $("#ename").val('');
                            try {
                                parent.layui.table.reload('roleList');
                            } catch (err) {
                            }
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        }, 2000);


                    } else {
                        layer.msg(data.message);

                    }
                },
                error: function (data) {
                    layer.msg(data.message);
                }
            });

            return false;
        });
    });
</script>

</body>
</html>